package jlb.andriva;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jlb.andriva.wrappers.Consumption;
import jlb.andriva.wrappers.ConsumptionTotal;
import jlb.andriva.wrappers.HttpTask;
import jlb.andriva.wrappers.Util;

public class ConsumptionActivity extends Fragment {

    private View rootView;
    private Element rootElem = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_consumption, container, false);

        if(rootElem == null){
        	if(LoginActivity.mTestMode == false){
        		ConsumptionsTask mConsumptionTask = new ConsumptionsTask();
        		mConsumptionTask.execute(getString(R.string.consumption_url));
        	}
        	else{
        		Document doc = org.jsoup.Jsoup.parse(Util.getRawContent(rootView.getContext(), R.raw.consumptions));
        		rootElem = doc.getElementById("monthlyBillPrintArea");
                if(rootElem != null){
                    paintResults();
                }
        	}
        }
        else{
            //Paint cache results
            paintResults();
        }

        return rootView;
    }

    private class ConsumptionsTask extends HttpTask {
        @Override
        protected void onPostExecute(final String result) {
            if (result != null) {
                Document doc = org.jsoup.Jsoup.parse(result);
                rootElem = doc.getElementById("monthlyBillPrintArea");
                if(rootElem != null){
                    paintResults();
                }
            }
        }
    }

    private void paintResults(){
        LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.consumption_list);
        LinearLayout llPanelTot = null;
        
        //TOTALS
        Elements trs = rootElem.select("tr[align=left]");
        
        LayoutInflater inflater = (LayoutInflater) rootView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        LinearLayout.LayoutParams rulerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);
        LinearLayout.LayoutParams llPanelTotParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        llPanelTotParams.setMargins(0, 0, 0, 10);
        
        llPanelTot = (LinearLayout) inflater.inflate(R.layout.llpanel, null);
        llPanelTot.setOrientation(LinearLayout.VERTICAL);
        llPanelTot.setLayoutParams(llPanelTotParams);
                
	    TextView lblTit = (TextView) inflater.inflate(R.layout.tvtitle, null);
	    lblTit.setGravity(Gravity.CENTER);
	    lblTit.setEllipsize(TextUtils.TruncateAt.END);
	    lblTit.setText("Totales");
	    
	    llPanelTot.addView(lblTit);
	    
	    View ruler = (View) inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(rulerParams);
        
        llPanelTot.addView(ruler);
        
        for(int i=1;i<trs.size();i++){
            Element tr = trs.get(i);
            ConsumptionTotal ctot = new ConsumptionTotal(tr);
            llPanelTot.addView(ctot.getView(rootView.getContext()));
        }
        
        ll.addView(llPanelTot);
        
        //CONSUMPTIONS
        trs = rootElem.getElementsByClass("coloredRows");
        String currentDate = "";
        LinearLayout llPanel = null;

        LinearLayout.LayoutParams llPanelParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        llPanelParams.setMargins(0, 0, 0, 10);
        for(int i=0;i<trs.size();i++){
            Element tr = trs.get(i);
            Consumption cons = new Consumption(tr);
            if(!currentDate.equals(cons.mFOperacion)){
                currentDate = cons.mFOperacion;

                llPanel = (LinearLayout) inflater.inflate(R.layout.llpanel, null);
                llPanel.setOrientation(LinearLayout.VERTICAL);
                llPanel.setLayoutParams(llPanelParams);
                
                TextView lblDate = (TextView) inflater.inflate(R.layout.tvnormal, null);
                lblDate.setGravity(Gravity.LEFT);
                lblDate.setEllipsize(TextUtils.TruncateAt.END);
                lblDate.setText(currentDate);

                llPanel.addView(lblDate);

                ll.addView(llPanel);
            }
            llPanel.addView(cons.getView(rootView.getContext()));
            //ll.addView(cons.getView(rootView.getContext()));
        }
    }
}